package com.android.camera.one;

import android.graphics.PointF;
import android.graphics.RectF;
import android.hardware.camera2.params.Face;

public class FaceDetectInfo {
	private RectF bounds;
	private PointF leftEye;
	private PointF rightEye;
	private PointF mouth;
    private int mScore;
    private int mId;
    
    private FaceDetectInfo() {}
    
    public static FaceDetectInfo generate(Face face, boolean front,
    		int dX, int dY, int offX, int offY, int sensorOrientation) {
    	FaceDetectInfo nFaceDetectInfo = new FaceDetectInfo();
    	
    	if (face.getLeftEyePosition() != null) {
    		nFaceDetectInfo.leftEye = new PointF();
    		nFaceDetectInfo.leftEye.x = (float) (face.getLeftEyePosition().x - offX) / dX;
    		nFaceDetectInfo.leftEye.y = (float) (face.getLeftEyePosition().y - offY) / dY;
        }
        if (face.getRightEyePosition() != null) {
        	nFaceDetectInfo.rightEye = new PointF();
        	nFaceDetectInfo.rightEye.x = (float) (face.getRightEyePosition().x - offX) / dX;
        	nFaceDetectInfo.rightEye.y = (float) (face.getRightEyePosition().y - offY) / dY;
        }
        if (face.getMouthPosition() != null) {
        	nFaceDetectInfo.mouth = new PointF();
        	nFaceDetectInfo.mouth.x = (float) (face.getMouthPosition().x - offX) / dX;
        	nFaceDetectInfo.mouth.y = (float) (face.getMouthPosition().y - offY) / dY;
        }
        if (face.getBounds() != null) {
        	nFaceDetectInfo.bounds = new RectF();
        	nFaceDetectInfo.bounds.left = (float) (face.getBounds().left - offX) / dX;
        	nFaceDetectInfo.bounds.top = (float) (face.getBounds().top - offY) / dY;
        	nFaceDetectInfo.bounds.right = (float) (face.getBounds().right - offX) / dX;
        	nFaceDetectInfo.bounds.bottom = (float) (face.getBounds().bottom - offY) / dY;
        }
        
        if (front && sensorOrientation == 90) {
        	nFaceDetectInfo.mirrorInY();
        }
        if (front && sensorOrientation == 270) {
        	nFaceDetectInfo.mirrorInX();
        }
        if (!front && sensorOrientation == 270) {
        	nFaceDetectInfo.mirrorInX();
        	nFaceDetectInfo.mirrorInY();
        }
        
        nFaceDetectInfo.mId    = face.getId();
        nFaceDetectInfo.mScore = face.getScore();
        
    	return nFaceDetectInfo;
    }
    
    public RectF getBounds() {
    	return bounds;
    }
    
    public PointF getLeftEye() {
    	return leftEye;
    }
    
    public PointF getRightEye() {
    	return rightEye;
    }
    
    public PointF getMouth() {
    	return mouth;
    }
    
    public int getId() {
    	return mId;
    }
    
    public int getScore() {
    	return mScore;
    }
    
    private void mirrorInX() {
        if (leftEye != null) {
            leftEye.x = 1f - leftEye.x;
        }
        if (rightEye != null) {
            rightEye.x = 1f - rightEye.x;
        }
        if (mouth != null) {
            mouth.x = 1f - mouth.x;
        }
        float oldLeft = bounds.left;
        bounds.left = 1f - bounds.right;
        bounds.right = 1f - oldLeft;
    }
    
    private void mirrorInY() {
        if (leftEye != null) {
            leftEye.y = 1f - leftEye.y;
        }
        if (rightEye != null) {
            rightEye.y = 1f - rightEye.y;
        }
        if (mouth != null) {
            mouth.y = 1f - mouth.y;
        }
        float oldTop = bounds.top;
        bounds.top = 1f - bounds.bottom;
        bounds.bottom = 1f - oldTop;
    }
}